package com.qzuser.sendmessagebasic.server.exchange;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qzuser.sendmessagebasic.common.exception.ToolException;

import java.util.HashMap;
import java.util.Map;

public class Exchange {
    private String name;
    private ExchangeType type = ExchangeType.DIRECT;
    private Boolean durable = false;
    private Boolean autoDelete = false;
    private Map<String, Object> arguments = new HashMap<String, Object>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ExchangeType getType() {
        return type;
    }

    public void setType(ExchangeType type) {
        this.type = type;
    }

    public Boolean getDurable() {
        return durable;
    }

    public void setDurable(Boolean durable) {
        this.durable = durable;
    }

    public Boolean getAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(Boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public void setArguments(Map<String, Object> arguments) {
        this.arguments = arguments;
    }

    public String getArguments() throws ToolException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            throw new ToolException("[Exchange] Map序列化为Json失败");
        }
    }

    public void setArguments(String objectJson) throws ToolException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            arguments = objectMapper.readValue(objectJson, new TypeReference<HashMap<String, Object>>() {});
        } catch (JsonProcessingException e) {
            throw new ToolException("[Exchange] Json序列化为Map失败");
        }
    }
}
